<template>
  <div class="con">
    <van-search
      v-model="keyWord"
      show-action
      placeholder="听什么呢"
      shape="round"
      @search="onSearch"
      @input="SearchSugg"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
      <template #left>
        <van-icon name="arrow-left" @click="goPush" />
      </template>
    </van-search>

    <div class="container" v-if="showBlock === 1">
      <div class="title">
        <h4>单曲</h4>
        <div class="more"><van-icon name="play-circle-o" />播放></div>
      </div>
      <ul class="songList">
        <li
          class="songItem van-ellipsis"
          v-for="item in songsList"
          :key="item.id"
          @click="goSongDetail(item.id)"
        >
          <div class="songInfo">
            <h5 class="songName">{{ item.name }}</h5>
            <p>{{ item.artists[0].name }}-{{ item.album.name }}</p>
          </div>
          <div class="right">
            <van-icon name="play-circle-o" />
            <i class="iconfont icon-gengduo-shuxiang"></i>
          </div>
        </li>
      </ul>
    </div>

    <div class="searchSugg" v-if="showBlock === 2">
      <ul class="searchSuggList">
        <li
          class="searchSuggItem"
          v-for="(item, index) in searchSuggList"
          :key="index"
          @click="handleSugg(item.keyword)"
        >
          <van-icon name="search" />
          <span class="van-ellipsis">{{ item.keyword }}</span>
        </li>
      </ul>
    </div>

    <div class="hotSearch" v-if="showBlock === 3">
      <div class="history" v-if="historyList.length">
        <h5>搜索历史</h5>
        <div class="historyList">
          <van-tag plain type="danger" v-for="(item,index) in historyList" :key="index">{{item}}</van-tag>
        </div>
      </div>

      <div class="recommend">
        <h5>推荐</h5>
        <div class="hotSearchCon">
          <div class="title">
            <h6>热搜榜</h6>
            <div class="more"><van-icon name="play-circle-o" />播放></div>
          </div>
          <ul class="hotSearchList">
            <li class="hotSearchItem" v-for="(item,index) in hotSearch" :key="index" @click="handleHotSearch(item.first)">
              <p>{{index+1}} <span>{{item.first}}</span></p>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { search, getSearchSugg,hotSearch } from "@/request/api";
export default {
  data() {
    return {
      keyWord: "",
      songsList: [],
      //   showBlock 1 代表单曲列表  2  搜索建议列表   3  热门列表
      showBlock: 3,
      searchSuggList: [],
      hotSearch: [],
      historyList:[]
    };
  },
  methods: {
    async onSearch(keywords) {
      // let keywords = this.data
      this.historyList.push(keywords)
      const { data: res } = await search({ keywords });
      console.log(res);
      if (res.code !== 200) return this.$toast("搜索失败");
      //搜索成功
      this.songsList = res.result.songs;
      this.showBlock = 1
      
    },
    goPush() {
      this.$router.go(-1);
    },
    goSongDetail(id) {
      this.$router.push({
        name: "SongDetail",
        query: {
          id,
        },
      });
    },
    async SearchSugg(value) {
      this.showBlock = 2
      if (!value) {
        return;
      }
      const { data: res } = await getSearchSugg({
        keywords: value,
        type: "mobile",
      });
      console.log(res);
      if (res.code !== 200) return this.$toast("请求失败");
      this.searchSuggList = res.result.allMatch;
    },
    handleSugg(keyword) {
      this.showBlock = 1;
      this.onSearch(keyword);
    },
    async getHotSearch(){
      const {data:res} = await hotSearch()
      console.log(res);
      this.hotSearch = res.result.hots
    },
    handleHotSearch(first){
      this.onSearch(first);
      this.showBlock = 1
    }
  },
  created(){
    this.getHotSearch()
  }
};
</script>

<style lang="less" scoped>
.con {
  padding: 10px;
}
.container {
  box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  padding: 0 10px;

  .title {
    display: flex;
    height: 50px;
    align-items: center;
    // background-color: red;
    justify-content: space-between;
    border-bottom: 1px solid #ccc;
    .more {
      padding: 5px 8px;
      font-size: 12px;
      border: 1px solid #bbb;
      border-radius: 15px;
      letter-spacing: 1px;
    }
  }
  .songList {
    .songItem {
      display: flex;
      //   width: 350px;
      height: 60px;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #ccc;
      .songInfo {
        .songName {
          font-size: 20px;
        }
        p {
          font-size: 13px;
        }
      }
      .right {
        font-size: 20px;
        i {
          font-size: 20px;
        }
      }
    }
  }
}
.searchSugg {
  .searchSuggList {
    .searchSuggItem {
      padding: 0 15px;
      font-size: 18px;
      line-height: 40px;
      border-bottom: 1px solid #eee;
    }
  }
}
.hotSearch {
  h5 {
    font-size: 18px;
  }
  .historyList {
    .van-tag {
      padding: 3px;
      margin: 5px;
    }
  }
  .recommend {
    margin-top: 15px;
    .hotSearchCon {
      .title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        // background-color: red;
        height: 45px;
        border-bottom: 1px solid #eee;
        .more {
          padding: 5px 8px;
          font-size: 12px;
          border: 1px solid #bbb;
          border-radius: 15px;
          letter-spacing: 1px;
        }
      }
      .hotSearchList{
        .hotSearchItem{
          line-height: 45px;
          border-bottom: 1px solid #eee;
        }
      }
    }
  }
}
</style>